var postcss = require('postcss')

module.exports = postcss.plugin('postcss-var-extract', function (/* opts */) {
  // opts = opts || {}

  // Work with options here

  return function (root/*, result */) {
    // Transform CSS AST here

    root.walkRules(function (rule) {
      rule.walkDecls(/^(background|border|color)-?/, function (decl) {
        if (!/var\(?/.test(decl.value)) return
        var defaultValue = decl.value.replace(/var(.*),\s*([^)]*)\)/, '$2').trim()
        console.log(decl.value, '-', defaultValue)
        if (!defaultValue) return
        rule.insertBefore(decl, {
          prop: decl.prop,
          value: defaultValue
        })
        /* rule.prepend({
          prop: decl.prop,
          value: defaultValue
        }) */
      })
    })
  }
})
